<template>
  <!-- 重点保护路段 -->
 <div class="targetStatisticsImportantRoad">
   <div ref="dom" class="dome"></div>
 </div>
</template>
<script  setup lang="ts">
import * as echarts from "echarts";
import { ref, watch, nextTick} from "vue";
// 接收值
let props = defineProps({
 carData: {
   type: Object,
   default: false,
 },
});
// 定义值
let dom = ref(); //ref的应用
// 定义加载函数
let initCharts = () => {
 let chart = echarts.init(dom.value);
 const option ={
        tooltip: {
          axisPointer: {
            type: 'shadow'
          },
          trigger: 'axis',
          textStyle: {
            align: 'left'
          }
        },
        legend: {
          itemWidth: 20,
          itemHeight: 10,
          textStyle: {
            color: '#90D4F8',
            align: 'left'
          }
        },
        grid: {
          top: '15%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: props.carData.name,
          axisLabel: {
            show: true,
            align: 'center',
            margin: 15,
            textStyle: {
              color: '#90D4F8'
            }
          },
          axisLine: {
            show: false,
            interval: 0,
            lineStyle: {
              color: '#468C9E',
              type: 'solid'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#213862',
              width: 1,
              type: 'solid'
            }
          }
        },
        yAxis: [
          {
            type: 'value',
            splitLine: {
              show: true,
              lineStyle: {
                color: '#213862',
                width: 1,
                type: 'solid'
              }
            },
            axisLine: {
              show: true,
              interval: 0,
              lineStyle: {
                color: '#468C9E',
                type: 'solid'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#90D4F8'
              }
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            data: props.carData.value,
            type: 'bar',
            name: '上月',
            barWidth: 10,
            symbolSize: 8,
            symbol: 'circle',
            itemStyle: {
              color: '#079FFF'
            }
          }
        ]
      };
 chart.setOption(option, true);
 // 当窗口大小改变时候，echarts重置大小
 window.addEventListener("resize", () => {
   chart.resize();
 });
};
// 监听当配置项发生变化的时候，调用加载函数
watch(
 () => props.carData,
 (val) => {
   if (Object.keys(val)) {
     nextTick(() => {
       initCharts();
     });
   }
 },
 { immediate: true }
);
</script>
<style scoped>
.dome,
.targetStatisticsImportantRoad {
 width: 100%;
 height: 100%;
}
</style>
